<template>
  <div>
    <div class="panel admin-panel">
      <div class="panel-head">
        <strong><span class="icon-pencil-square-o"></span>公告查询</strong>
      </div>
      <tr>
        <td>
          <el-input
            placeholder="标题"
            v-model="title"
            clearable>
          </el-input>
        </td>
        <td>
          <div class="block">
            <el-date-picker
              v-model="pubDate"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </div>
        </td>
        <td>
          <el-button type="primary" @click="query">查询</el-button>
        </td>
      </tr>
    </div>
    <div class="panel admin-panel">
      <div class="panel-head">
        <strong><span class="icon-pencil-square-o"></span>消息列表</strong>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        style="width: 100%"
        @selection-change="handleSelectionChange">
        <el-table-column
          type="selection">
        </el-table-column>
        <el-table-column
          prop="status"
          label="状态">
        </el-table-column>
        <el-table-column
          prop="title"
          label="标题">
        </el-table-column>
        <el-table-column
          prop="pubDate"
          label="时间">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleView(scope.$index, scope.row)">查看
            </el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
    <el-dialog
      title="详细信息"
      :modal=false
      :cente=true
      :destroy-on-close=true
      :visible.sync="dialogVisible"
      top="20vh"
      width="50%">
      <span style="color: #00aa00">标题：{{titleView}}</span><br/>
      <span>内容：{{content}}</span>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
  import axios from "axios"

  export default {
    data() {
      return {
        dialogVisible: false,
        titleView: '',
        content: '',
        title: '',
        pubDate: '',
        tableData: [],
        total: 0,
        currentPage: 1,
        pageSize: 0,
      }
    },
    methods: {
      handleView(index, row) {
        let that = this;
        console.log(row)
        this.dialogVisible = true
        this.titleView = row.title;
        this.content = row.content;
      },
      handleDelete(index, row) {
        let that = this;
        this.$confirm('此操作将删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
          beforeClose: (action, instance, done) => {
            if (action === 'confirm') {
              instance.confirmButtonLoading = true;
              instance.confirmButtonText = '执行中...';
              setTimeout(() => {
                console.log(index, row.id);
                axios.get(this.apiUrl + "/notice/del/" + row.id).then(function (r) {
                  if (r.data.code == "200") {
                    that.tableData.splice(index, 1)
                    done();
                  }
                })
                setTimeout(() => {
                  instance.confirmButtonLoading = false;
                }, 300);
              }, 3000);
            } else {
              done();
            }
          }
        }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'

          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      toggleSelection(rows) {
        if (rows) {
          rows.forEach(row => {
            this.$refs.multipleTable.toggleRowSelection(row);
          });
        } else {
          this.$refs.multipleTable.clearSelection();
        }
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
        console.log(val)
      },
      query() {
        let that = this
        axios({
          url: this.apiUrl + "/notice/list",
          method: "post",
          data: JSON.stringify({
            "title": this.title,
            "pubDate": this.pubDate[0],
            "pubDate_max": this.pubDate[1],
            "page": 1,
          }),
          headers: {"Content-Type": "application/json"}
        }).then(function (r) {
          that.tableData = r.data.data.list;
          that.total = r.data.data.total;
          that.currentPage = r.data.data.pageNum;
          that.pageSize = r.data.data.pageSize;
        });
      },
      handleSizeChange(val) {
        console.log(`每页 ${val} 条`);
      },
      handleCurrentChange(val) {
        console.log(val)
        let that = this
        axios({
          url: this.apiUrl + "/notice/list",
          method: "post",
          data: JSON.stringify({
            "title": this.title,
            "pubDate": this.pubDate[0],
            "pubDate_max": this.pubDate[1],
            "page": val,
          }),
          headers: {"Content-Type": "application/json"}
        }).then(function (r) {
          that.tableData = r.data.data.list;
          that.total = r.data.data.total;
          that.currentPage = r.data.data.pageNum;
          that.pageSize = r.data.data.pageSize;
        });
      },

    }
    ,
    mounted() {
      axios.post(this.apiUrl + "/notice/list", {page: 1}).then(r => {
        console.log(r)
        if (r.data.code === "200") {
          this.tableData = r.data.data.list;
          this.total = r.data.data.total;
          this.currentPage = r.data.data.pageNum;
          this.pageSize = r.data.data.pageSize;
        }
      })
    }
  }
</script>

<style scoped>
  .el-input__inner {
    width: 400px;
  }
</style>
